
import { fetchHome } from '../../../services/home/home';
import { fetchGoodsList } from '../../../services/good/fetchCategoryList';
import { getSelectedSchoolId } from '../../../userData/userData'
import Toast from 'tdesign-miniprogram/toast/index';
import { getRight } from '../../../userData/userData';

Page({
  data: {
    sideBarIndex: 0,
    scrollTop: 0,
    allGoods: [],
    categories: [],
  },


  onShow() {
    this.getTabBar().init();
  },

  onLoad() {
    this.init();
  },

  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },

  onPullDownRefresh() {
    this.init();
  },

  init() {
    // this.fetchGoodsList();
    fetchGoodsList().then(res => {
      let item0 = [];
      let item1 = [];
      let item2 = [];
      let item3 = [];
      if (res && res.length > 0) {
        res.forEach(ele => {
          switch (ele.tabType) {
            case 0:
              item0.push(ele)
              break;
            case 1:
              item1.push(ele)
              break;
            case 2:
              item2.push(ele)
              break;
            case 3:
              item3.push(ele)
              break;
            default:
              break;
          }
        })
      }
      let s = [
        // {
        //   label: '未定义',
        //   title: '未定义',
        //   id: 0,
        //   items: item0
        // },
        {
          label: '保安服',
          title: '保安服',
          id: 1,
          items: item1
        },
        {
          label: '衬衣西装',
          title: '衬衣西装',
          id: 2,
          items: item2
        },
        {
          label: '工作服',
          title: '工作服',
          id: 3,
          items: item3
        }
      ];
      this.setData({
        categories: s
      })
    })
  },
  //深拷贝
  deepClone(obj) {
    if (typeof obj !== "object" || obj == null) {
      return obj
    }
    let res
    if (obj instanceof Array) {
      res = []
    } else {
      res = {}
    }
    for (let key in obj) {
      if (obj.hasOwnProperty(key)) {
        res[key] = this.deepClone(obj[key])
      }
    }
    return res
  },
  //展示商品图片
  showDetailImg(e) {
    const { image } = e.target.dataset
    console.log(image)
    this.setData({
      images: [
        image
      ],
      // visible: true,
    });
    wx.previewImage({
      urls: this.data.images
    })
  },
  loadHomePage() {
    wx.stopPullDownRefresh();
    this.setData({
      pageLoading: true,
    });
    this.setData({
      isVisiter: getRight() == 1
    })
    console.log(this.data.isVisiter)
    fetchHome().then(({ swiper, tabList }) => {
      this.setData({
        tabList,
        imgSrcs: swiper,
        pageLoading: false,
      });
      this.loadGoodsList(true);
    });
  },

  tabChangeHandle(e) {
    this.privateData.tabIndex = e.detail;
    this.loadGoodsList(true);
  },

  onReTry() {
    this.loadGoodsList();
  },

  async loadGoodsList(fresh = false) {
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
    }

    this.setData({ goodsListLoadStatus: 1 });

    const pageSize = this.goodListPagination.num;
    let pageIndex = this.privateData.tabIndex * pageSize + this.goodListPagination.index + 1;
    if (fresh) {
      pageIndex = 0;
    }

    try {
      const nextList = await fetchGoodsList(pageIndex, pageSize);
      let tempGoodList = this.getFilter(fresh ? nextList : this.data.goodsList.concat(nextList))
      this.setData({
        goodsList: tempGoodList,
        goodsListLoadStatus: 0,
      });

      this.goodListPagination.index = pageIndex;
      this.goodListPagination.num = pageSize;
    } catch (err) {
      this.setData({ goodsListLoadStatus: 3 });
    }
  },
  navToSearchPage() {
    wx.navigateTo({ url: '/pages/goods/search/index' });
  },

  navToActivityDetail({ detail }) {
    const { index: promotionID = 0 } = detail || {};
    wx.navigateTo({
      url: `/pages/promotion-detail/index?promotion_id=${promotionID}`,
    });
  },
  //popup框显隐
  onVisibleChange() {
    this.setData({
      visible: !this.data.visible
    });
    if (this.data.visible) {
      this.setData({
        number: 1,
        currentSize: 0
      })
    }
  },
  //选择尺码
  onChangeSize(e) {
    const { num } = e.target.dataset
    this.setData({
      currentSize: num
    })
  },
  onSideBarChange(e) {
    const { value } = e.detail;
    this.setData({ sideBarIndex: value });
  },
  changeHandle(e) {
    const { value } = e.detail;
    const goodList = this.data.tempList ? this.data.tempList : this.deepClone(this.data.categories)
    for (let index = 0; index < goodList.length; index++) {
      // debugger
      this.data.categories[index].items = this.getFilter(goodList[index].items, value)

    }
    this.setData({
      tempList: goodList,
      categories: this.data.categories
    })
  },
  clearHandle(e) {
    const goodList = this.data.tempList
    if (goodList) {
      this.setData({
        categories: this.data.tempList,
        tempList: undefined,
      })
    }
  },
  getFilter(data, filter) {
    let resultArr = []
    if (filter) {
      resultArr = data.filter(item => {
        if (item.title.indexOf(filter) >= 0)
          return item
      })
      return resultArr
    }
    else
      return data
  }
});

